﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

namespace Common
{
    public class UIHelper
    {
        private System.Windows.Forms.Screen[] _screens = null;
        private static UIHelper _instance = null;
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static UIHelper Instance()
        {
            if (_instance == null)
            {
                _instance = new UIHelper();
            }
            return _instance;
        }
        /// <summary>
        /// 获取所有屏幕
        /// </summary>
        /// <returns></returns>
        public System.Windows.Forms.Screen[] GetScreens()
        {
            _screens = System.Windows.Forms.Screen.AllScreens;
            return _screens;
        }
        /// <summary>
        /// 移动到下一个焦点
        /// </summary>
        public void MoveNextFocus() {
            TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

            UIElement focusElement = Keyboard.FocusedElement as UIElement;
            if (focusElement != null)
            {
                focusElement.MoveFocus(request);
            }
        }
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos", SetLastError = true)]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
        /// <summary>
        /// 指定屏幕分配指定窗体
        /// </summary>
        /// <param name="screen">屏幕</param>
        /// <param name="win">窗体</param>
        /// <returns>操作成功</returns>
        public bool ShowToScreen(System.Windows.Forms.Screen screen, Window win)
        {
            try
            {
                WindowInteropHelper wndHelper = new WindowInteropHelper(win);
                IntPtr wpfHwnd = wndHelper.Handle;
                bool show = SetWindowPos(wpfHwnd, (IntPtr)(0), screen.Bounds.Location.X, screen.Bounds.Location.Y, screen.Bounds.Width, screen.Bounds.Height, 0x0040);
                if (show)
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                LogHelper.Error(typeof(UIHelper), e.Message);
            }
            return false;
        }
        /// <summary>
        /// 滚轮滚动响应
        /// </summary>
        /// <param name="_patients"></param>
        /// <param name="delta"></param>
        public static void Scroll(System.Windows.Controls.ListView _patients, int delta)
        {
            ListViewAutomationPeer lvap = new ListViewAutomationPeer(_patients);
            var svap = lvap.GetPattern(PatternInterface.Scroll) as ScrollViewerAutomationPeer;
            var scroll = svap.Owner as ScrollViewer;
            scroll.ScrollToHorizontalOffset(scroll.HorizontalOffset - delta);
        }
        /// <summary>
        /// 将list选中项滚动居中
        /// </summary>
        /// <param name="_videos"></param>
        public static void ScrollToCenter(ListView _videos)
        {
            double wid = SystemParameters.PrimaryScreenWidth;
            double hei = SystemParameters.PrimaryScreenHeight;
            ListViewAutomationPeer lvap = new ListViewAutomationPeer(_videos);
            var svap = lvap.GetPattern(PatternInterface.Scroll) as ScrollViewerAutomationPeer;
            var scroll = svap.Owner as ScrollViewer;
            double y = (hei - 55.0) * (785.0 / (785.0 + 60.0 + 180.0));
            double pw = y * 1145.0 / 785.0;
            int x = (int)(pw - (wid - pw) / 2);
            scroll.ScrollToHorizontalOffset(x);
        }
        /// <summary>
        /// 寻找指定类型的子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<ChildType> FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            try
            {
                List<ChildType> list = new List<ChildType>();
                int num = VisualTreeHelper.GetChildrenCount(obj);
                if (num > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                        if (child != null && child is ChildType)
                        {
                            list.Add(child as ChildType);
                        }
                        List<ChildType> childOfChildren = FindVisualChild<ChildType>(child);
                        if (childOfChildren != null && childOfChildren.Count > 0)
                        {
                            list.AddRange(childOfChildren);
                        }
                    }
                }
                return list;
            }
            catch (Exception)
            {
                return new List<ChildType>();
            }
        }
        /// <summary>
        /// 获取对象地址
        /// </summary>
        /// <param name="StreamIP"></param>
        /// <returns></returns>
        internal static IntPtr GetIntPtr(Object StreamIP)
        {
            GCHandle hander = GCHandle.Alloc(StreamIP);
            return GCHandle.ToIntPtr(hander);
        }
    }
}
